from db.db_base import DBBase, commit, get_one_value, get_one_dict, get_all_dict
from boot import Dict, List , set_json_config, get_json_config
import json


__all__ = ["DBSale"]


class DBSale(DBBase):

    @commit
    def add_info(self, sale_id: str, cust_id: str, sale_datetime: str, sale_list: str):
        return """
        INSERT INTO `Sales` 
        (`sale_id`, `cust_id`, `sale_datetime`, `sale_list`)
        VALUES (?, ?, ?, ?);
        """

    @commit
    def delete_info(self, sale_id: str):
        # 删除信息
        return """
        DELETE FROM `Sales`
        WHERE `sale_id` = ?;
        """

    @commit
    def update_sale_info(self, cust_id: str, sale_datetime: str, sale_list: str, sale_id: str):
        # 更新信息
        return """
        UPDATE `Sales`
        SET `cust_id` = ?, `sale_datetime` = ?, `sale_list` = ?
        WHERE `sale_id` = ?;
        """

    @get_all_dict("Sales")
    def get_all_info(self, cust_id: str):
        return """
        SELECT *
        FROM `Sales`
        WHERE `cust_id` = ?
        ORDER BY `sale_datetime` DESC;
        """

    @get_one_dict("Sales")
    def get_info(self, sale_id: str):
        # 获取一条信息
        return """
        SELECT *
        FROM `Sales`
        WHERE `sale_id` = ?;
        """

    @get_one_value
    def get_cust_id_by_id(self, sale_id: str):
        # 获取客户id
        return """
        SELECT `cust_id`
        FROM `Sales`
        WHERE `sale_id` = ?;
        """